package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.Entity;
import cn.com.ddapp.ddm.bean.mine.Addresses;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.mine.AddAddressActivity;
import cn.com.ddapp.ddm.ui.mine.AddressEdtActivity;
import cn.com.ddapp.ddm.util.SPUtils;
import cn.com.ddapp.ddm.widget.TipsToast;


/**
 * User: Administrator
 * Date: 2016/7/28 0028
 * Time: 16:53
 */
public class AddressAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_FOOTER = 1;
	private Context context;
	private LayoutInflater inflater;
	private List<Addresses.ListBean> list;
	public boolean isLoading = false;
	private boolean mIfTable;

	String userId;


	public AddressAdapter(Context context, List<Addresses.ListBean> list,boolean ifTable) {
		this.context = context;
		this.list = list;
		inflater = LayoutInflater.from(context);
		userId= SPUtils.getStringData(context, SPUtils.USERID);
		this.mIfTable = ifTable;
	}

	@Override
	public int getItemViewType(int position) {
		if (position + 1 == getItemCount()) {
			return TYPE_FOOTER;
		} else {
			return TYPE_ITEM;
		}
	}

	public void clearAllCheck() {
		for (int i = 0; i < list.size(); i++) {
			list.get(i).setIsSelect(0);
		}
		notifyDataSetChanged();
	}

	public int getCheck() {
		int result = -1;
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).getIsSelect() == 1) {
				result = i;
			}
		}
		return result;
	}

	private OnRecyclerViewItemClickListener mOnItemClickListener = null;

	//define interface
	public static interface OnRecyclerViewItemClickListener {
		void onItemClick(View view, int position, ItemViewHolder holder);
	}

	public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
		this.mOnItemClickListener = listener;
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

		if (viewType == TYPE_ITEM) {
			View view = inflater.inflate(R.layout.item_shipping_address, parent, false);
			final ItemViewHolder viewHolder = new ItemViewHolder(view);
			view.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					if (mOnItemClickListener != null) {
						//注意这里使用getTag方法获取数据
						mOnItemClickListener.onItemClick(view, (int) view.getTag(), viewHolder);
					}
				}
			});
			return viewHolder;
		} else if (viewType == TYPE_FOOTER) {
			View view = LayoutInflater.from(context).inflate(R.layout.item_add_btn, parent,
					false);
			if(mIfTable&&list.size()>0){
				view.setVisibility(View.GONE);
			}
			return new FootViewHolder(view);
		}
		return null;
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

		if (holder instanceof ItemViewHolder) {
			ItemViewHolder holder1 = (ItemViewHolder) holder;
			holder1.tv_shipping_address_name.setText(list.get(position).getName());
			holder1.tv_shipping_address_phone_num.setText(list.get(position).getPhone());
			holder1.tv_shipping_address.setText(list.get(position).getProvince() + list.get(position).getCity() + list.get(position).getCounty() + list.get(position).getAddress());
			holder1.itemView.setTag(position);
			if (list.get(position).getIsSelect() == 1) {
				holder1.shipping_address_default.setChecked(true);
			} else {
				holder1.shipping_address_default.setChecked(false);
			}

			holder1.tv_shipping_address_delete.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					deleteAddress(list.get(position).getId(), position);
				}
			});
			holder1.shipping_address_default.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					SetDefault(list.get(position).getId(), position);

				}
			});

			holder1.tv_shipping_address_edit.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {

					Intent intent = new Intent(context, AddressEdtActivity.class);
					Bundle mBundle = new Bundle();
					mBundle.putSerializable("ddm.address",list.get(position));
					intent.putExtras(mBundle);
					context.startActivity(intent);

				}
			});

		}
		if (holder instanceof FootViewHolder) {
			FootViewHolder holder2 = (FootViewHolder) holder;

			holder2.add_btn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Intent intent = new Intent(context, AddAddressActivity.class);
					intent.putExtra("flag", "address");
					context.startActivity(intent);

				}
			});

		}
	}


    //设置默认地址
	private void SetDefault(int id, final int position) {
		clearAllCheck();
		OkUtilCallback callback = new OkUtilCallback() {
			@Override
			public void onMyResponse(String response, int id) {
				Gson gson = new Gson();
				Entity result = gson.fromJson(response, Entity.class);
				if (result.getResult().equals("OK")) {
					TipsToast.makeText(context, R.string.successful_setdefault, TipsToast.LENGTH_SHORT).show();
					list.get(position).setIsSelect(1);
                    SPUtils.setStringData(context,SPUtils.REFID,list.get(position).getRefId()+"");
					notifyDataSetChanged();
				} else {
					TipsToast.makeText(context, result.getResult(), TipsToast.LENGTH_SHORT).show();
				}
			}
		};
		OKUtilAPI.post(OKUtilAPI.setDefaultAddress,"userId="+userId+"&id="+id,callback);

	}

    //删除地址
	private void deleteAddress(int id, final int position) {
		OkUtilCallback callback = new OkUtilCallback() {
			@Override
			public void onMyResponse(String response, int id) {
				Gson gson = new Gson();
				Entity result = gson.fromJson(response, Entity.class);
				if (result.getResult().equals("OK")) {
					list.remove(position);
					notifyDataSetChanged();
				} else {
					TipsToast.makeText(context, result.getResult(), TipsToast.LENGTH_SHORT).show();
				}
			}
		};
		OKUtilAPI.post(OKUtilAPI.deleteAddress,"userId="+userId+"&id="+id,callback);

	}

	@Override
	public int getItemCount() {
		return list.size() + 1;
	}

	public class ItemViewHolder extends RecyclerView.ViewHolder {
		@BindView(R.id.shipping_address_name)
		TextView tv_shipping_address_name;
		@BindView(R.id.shipping_address_phone_num)
		TextView tv_shipping_address_phone_num;
		@BindView(R.id.shipping_address)
		TextView tv_shipping_address;
		@BindView(R.id.shipping_address_edit)
		TextView tv_shipping_address_edit;
		@BindView(R.id.shipping_address_delete)
		TextView tv_shipping_address_delete;
		@BindView(R.id.shipping_address_default)
		CheckBox shipping_address_default;

		ItemViewHolder(View view) {
			super(view);
			AutoUtils.autoSize(view);
			ButterKnife.bind(this, view);
		}
	}

	static class FootViewHolder extends RecyclerView.ViewHolder {

		@BindView(R.id.add_btn)
		TextView add_btn;

		public FootViewHolder(View view) {
			super(view);
			AutoUtils.autoSize(view);
			ButterKnife.bind(this, view);
		}
	}

}
